package edu.usx.server.controller;

import edu.usx.server.pojo.MsgContent;
import edu.usx.server.pojo.PageBean;
import edu.usx.server.pojo.RespBean;
import edu.usx.server.pojo.User;
import edu.usx.server.service.IMsgService;
import edu.usx.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author  聂枫
 * @since 2024-07-29
 */


@RestController
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    private IMsgService imsgService;
    @Autowired
    private IUserService iUserService;



    @GetMapping("content")//获取内容
    public RespBean getMsg(@RequestParam Integer pageNum,@RequestParam Integer pageSize,Principal principal)
    {
        if(principal==null)
        {
            return null;
        }

       String username = principal.getName();
        //根据用户名获取用户完整对象
       User user = iUserService.getUserByUserName(username);
       user.setPassword(null);

       PageBean<MsgContent> pageBean=imsgService.findMsgByUserId(pageNum,pageSize,user.getId());

       return RespBean.success("请求成功",pageBean);

    }

    @PutMapping("read")//一键已读
    public RespBean updateAllMsgRead(Principal principal) {
        if(principal==null)
        {
            return null;
        }

        String username = principal.getName();
        //根据用户名获取用户完整对象
        User user = iUserService.getUserByUserName(username);
        user.setPassword(null);

        imsgService.updateAllMsgReadByUserId(user.getId());

        return RespBean.success("一键已读");
    }
}
